var $ = layui.jquery,
    layer = layui.layer;

function serverUrl(uri) {
    return baseURL + "/" + uri;
}

function initSearchForm(formId, tableId) {
    // 监听搜索操作
    layui.form.on('submit(form-search-btn)', function (data) {
        //执行搜索重载
        layui.table.reload(tableId, {
            page: {curr: 1},
            where: data.field
        }, 'data');
        return false;
    });
    layui.form.on('submit(form-reset-btn)', function (data) {
        layui.form.reset();
        //执行搜索重载
        layui.table.reload(tableId, {
            page: {curr: 1},
            where: {}
        }, 'data');
        return false;
    });
}

function openFormView(uri, title) {
    layer.open({
        title: title,
        type: 2,
        shade: 0.5,
        maxmin:true,
        shadeClose: true,
        area: ['95%', '90%'],
        content: serverUrl(uri),
        btn: ['确认保存', '关闭6返回'],
        btnAlign: 'c',
        yes: function(index, layero){
            var submit = layero.find('iframe').contents().find("#commitBtn");
            submit.click();
            return false;
        },
        btn2: function(index){
            layer.close(index);
        }
    });
}
function openlistView(uri, title) {
    layer.open({
        title: title,
        type: 2,
        shade: 0.5,
        maxmin:true,
        shadeClose: true,
        area: ['95%', '90%'],
        content: serverUrl(uri),
        btn: ['确认保存', '关闭5返回'],
        btnAlign: 'c',
        yes: function(index, layero){
            var submit = layero.find('iframe').contents().find("#commitBtn");
            submit.click();
            return false;
        },
        btn2: function(index){
            layer.close(index);
        }
    });
}

function openPrompt(title, tips, callback) {
    layer.prompt({
        title: '提示',
        content:'<div class="layui-layer-padding"><i class="layui-layer-ico layui-layer-ico3"></i>' + title + '</div>' +
            '<textarea class="layui-layer-input" style="width: 320px; height: 80px;" placeholder="' + tips + '"></textarea>'
    },function(value, index, elem){
        callback(value);
        layer.close(index);
    });
}

function submitFormData(uri, params, func) {
    var loadLayer = layer.load();
    var _parent = parent;
    $.ajax({
        type: "POST",
        url: serverUrl(uri),
        data: JSON.stringify(params),
        contentType: 'application/json',
        success: function (result) {
            layer.close(loadLayer);
            if (result.code === 0) {
                layer.msg('保存成功', {icon: 1, time: 1000}, function(){
                    var iframeIndex = _parent.layer.getFrameIndex(window.name);
                    _parent.layui.table.reload('currentTableId');
                    _parent.layer.close(iframeIndex);
                    if (func) func(result);
                });
            } else {
                layer.msg(result.msg, {icon: 2});
            }
        },
        error: function (req, msg) {
            layer.close(loadLayer);
            layer.msg(msg, {icon: 2});
        }
    });
}

function deleteTableRow(uri, data, func) {
    var loadLayer = layer.load();
    $.ajax({
        type: "POST",
        url: serverUrl(uri),
        data: JSON.stringify(data),
        contentType: 'application/json',
        success: function (result) {
            layer.close(loadLayer);
            if (result.code === 0) {
                if (func) func(result);
            } else {
                layer.msg(result.msg, {icon: 2});
            }
        },
        error: function (req, msg) {
            layer.close(loadLayer);
            layer.msg(msg, {icon: 2});
        }
    });
}
